package com.bpf.base.util;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SerialNo {
	public SerialNo() {
	}

	public static synchronized String getUNID() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");

		String currentTime = sdf.format(new Date());
		if (compareTime == null || compareTime.compareTo(currentTime) != 0) {
			compareTime = currentTime;
			sequence = 1L;
		} else {
			sequence++;
		}
		return (new StringBuilder(String.valueOf(currentTime))).append(numberFormat.format(sequence)).toString();
	}

	private static long sequence;
	private static String compareTime;
	private static NumberFormat numberFormat;
	private static NumberFormat smallFormat;

	private static NumberFormat longFormat;

	static {
		numberFormat = NumberFormat.getInstance();
		numberFormat.setGroupingUsed(false);
		numberFormat.setMinimumIntegerDigits(5);
		numberFormat.setMaximumIntegerDigits(5);
		smallFormat = NumberFormat.getInstance();
		smallFormat.setGroupingUsed(false);
		smallFormat.setMinimumIntegerDigits(4);
		smallFormat.setMaximumIntegerDigits(4);
		longFormat = NumberFormat.getInstance();
		// 是否允许分组 1123456=》1,123,456
		longFormat.setGroupingUsed(false);
		// 允许小整数位数
		longFormat.setMinimumIntegerDigits(13);
		// 允许大整数位数
		longFormat.setMaximumIntegerDigits(13);
	}

	public static void main(String[] args) {
		String pk = SerialNo.getUNID();
		System.out.println(pk);
		System.out.println(SerialNo.getUNID());
	}
}
